var ORDER_SUMMARY_INTERVAL = 10000;

$(function() {
	$("#accordion").accordion({
		collapsible : true
	});

	$(".product-box-dialog").dialog({
		autoOpen : false,
		modal : true,
		minHeight : 115,
		width : 455
	});

	$(".product-box-dialog-opener").click(function() {
		var $id = $(this).attr('href');

		$($id).dialog("open");
	});

	$('.add-product').click(
			function() {
				var elem = $(this);
				var counterElem = $(elem).parent().prev();
				var qtyHidden = $(elem).parent().siblings().filter(
						'#product_qty');
				var counter = $(counterElem).text();
				var maxValue = $(elem).parent().parent().next().children()
						.filter('.remaining-stock-parent').children().filter(
								'.remaining-stock-value').text();

				counter = parseInt(counter);
				maxValue = parseInt(maxValue);

				if (counter < maxValue) {
					counter += 1;

					$(counterElem).text(counter);
					$(qtyHidden).val(counter);
				}
			});

	$('.minus-product').click(function() {
		var elem = $(this);
		var counterElem = $(elem).parent().prev();
		var qtyHidden = $(elem).parent().siblings().filter('#product_qty');
		var counter = $(counterElem).text();

		counter = parseInt(counter);

		counter -= 1;

		if (counter < 0) {
			counter = 0;
		}

		$(counterElem).text(counter);
		$(qtyHidden).val(counter);
	});

	callOrderSummary();

	// setInterval(function () {callOrderSummary();} , ORDER_SUMMARY_INTERVAL);

	$('.button-add-order').click(
			function() {
				var elem = $(this);
				var prod_qty = $(elem).parent().parent().prev().children()
						.filter('#product_qty').val();
				var prod_id = $(elem).parent().parent().prev().children()
						.filter('#product_id').val();
				var parent_id = $(elem).parent().parent().parent().parent()
						.attr('id');

				var request = {
					product_id : prod_id,
					product_qty : prod_qty
				};

				if (prod_qty > 0) {
					var url = getUrl('sales', 'addOrder');

					$.post(url, request, function() {

					});

					$("#" + parent_id).dialog("close");
				}

				callOrderSummary();
			});

	$('.button-update-order').click(
			function() {
				var elem = $(this);
				var prod_qty = $(elem).parent().parent().prev().children()
						.filter('#product_qty').val();
				var prod_id = $(elem).parent().parent().prev().children()
						.filter('#product_id').val();

				var request = {
					product_id : prod_id,
					product_qty : prod_qty
				};

				if (prod_qty > 0) {
					var url = getUrl('sales', 'updateOrder');

					$.post(url, request, function() {

					});

					$("#product_qty_edit").dialog("close");
					callOrderSummary();
				}
			});

	$('#completeOrder').click(
			function() {
				$("#confirmation-message").text(
						"Are you sure you want to process order?");
				$("#confirmation-box").dialog({
					buttons : {
						"Yes" : function() {
							$(this).dialog("close");
							completeOrder();
						},
						"No" : function() {
							$(this).dialog("close");
						}
					}
				});
				$("#confirmation-box").dialog("open");
			});

	$("#product_receipt").dialog({
		autoOpen : false,
		modal : true,
		minHeight : 115,
		width : 455
	});
});

function callOrderSummary() {
	var url = getUrl('sales', 'orderSumarry');

	$.post(url, function(response) {
		$("#orderSumarryBoxList").html(response);
	});
}

function showName(prodId) {
	hideName();
	$("#prodName_" + prodId).slideDown();
}

function hideName() {
	$(".product-image-name").slideUp();
}

function completeOrder() {
	$("#loading-box").dialog("open");

	var url = getUrl('sales', 'proccessCompleteOrder');

	$.post(url, function(response) {
			
		var obj = jQuery.parseJSON(response);
		var receiptTrigger = true;

		if (obj.result) {

			if (!receiptTrigger) {
				 var url2 = getUrl('employee', 'home');

				 window.location = url2;
			} else {
								
				var total_price = 0;
				$("#salesDetail").html("<tr><th>Product Name</th><th>Product QTY</th><th>Product Price</th></tr>");
				
				for ( var i = 0; i < obj.product.length; i++) {
					var product_name = obj.product[i].product_name;
					var product_price = obj.product[i].product_price;
					var product_qty = obj.product[i].product_qty;
	
					var product_real_price = product_price * product_qty;
	
					total_price += product_real_price;
	
					var innerDiv = "<tr><td>"+product_name+"</td><td>"+product_qty+"</td><td>"+product_price+"</td></tr>";
	
					$("#salesDetail").append(innerDiv);
				}
				
				var innerDiv = "<tr><td colspan='3'><hr /></td></tr><tr><td style='font-weight:bold;color:red;'>TOTAL</td><td>&nbsp;</td><td style='font-weight:bold;color:red;'>"+total_price+"</td></tr><tr><td colspan='3'><hr /></td></tr>";
				
				$("#salesDetail").append(innerDiv);
				
				
				$("#loading-box").dialog("close");
				var url = getUrl('sales', 'index');
				window.location.href = url;
				window.print();
				//$("#product_receipt").dialog("open");
			}
		} else {
			$('#error-message').text(obj.message);
			$("#error-box").dialog('open');
		}

		$("#loading-box").dialog("close");
	});
}

function getProductInfo(product_id) {
	$("#loading-box").dialog("open");

	var request = {
		'product_id' : product_id
	};

	var url = getUrl('sales', 'getCurrentProductInfo');

	$.post(url, request, function(response) {
		var obj = jQuery.parseJSON(response);
		var $product_edit = $('#product_edit').children();

		$($product_edit).filter('#product_qty').val(obj.current_qty);
		$($product_edit).filter('#product_id').val(obj.product_id);
		$('#edit_product_qty_text').text(obj.current_qty);
		$('#edit_product_name').text(obj.product_name);
		$('#edit_product_price').text(obj.product_price);
		$('#edit_current_count').text(obj.product_current_qty);

		$("#loading-box").dialog("close");
		$("#product_qty_edit").dialog("open");
	});
}

function deleteProduct(product_id) {
	$("#loading-box").dialog("open");

	var request = {
		'product_id' : product_id
	};

	var url = getUrl('sales', 'removeProduct');

	$.post(url, request, function(response) {
		$("#loading-box").dialog("close");
		callOrderSummary();
	});
}